package org.example.practiceexamples.mapper;

import org.example.practiceexamples.dox.Appointment;
import org.example.practiceexamples.dox.User;
import org.example.practiceexamples.dto.UserAppointment;
import org.springframework.jdbc.core.ResultSetExtractor;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class UserAppointmentResultSetExtractor implements ResultSetExtractor<UserAppointment> {

    @Override
    public UserAppointment extractData(ResultSet rs) throws SQLException{
        User user = null;
        List<Appointment> appointments = new ArrayList<>();
        while (rs.next()) {
            if(user == null) {
                user = User.builder()
                        .id(rs.getString("u.id"))
                        .name(rs.getString("name"))
                        .phone(rs.getString("phone"))
                        .description(rs.getString("description"))
                        .role(rs.getString("role"))
                        .status(rs.getInt("status"))
                        .account(rs.getString("account"))
                        .password(rs.getString("password"))
                        .profession(rs.getString("profession"))
                        .updateTime(rs.getObject("update_time", LocalDateTime.class))
                        .createTime(rs.getObject("create_time", LocalDateTime.class))
                        .build();
            }
            Appointment a = Appointment.builder()
                    .id(rs.getString("a.id"))
                    .course(rs.getString("course"))
                    .teacher(rs.getString("teacher"))
                    .labId(rs.getString("lab_id"))
                    .labName(rs.getString("lab_name"))
                    .week(rs.getInt("week"))
                    .dayofweek(rs.getInt("dayofweek"))
                    .section(rs.getInt("section"))
                    .build();
            appointments.add(a);

        }
        return UserAppointment.builder()
                .appointments(appointments)
                .user(user)
                .build();
    }
}
